﻿Imports Mysoft.Map.Application.Security
Imports Mysoft.Map.Data
Imports Mysoft.Map.Caching

Partial Public Class MyDesktop_FunctionTree
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        repFunction.BindMode = 1
        repFunction.DataTable = GetFunctionDataTable()
        repFunction.DataBind()
    End Sub
    '检测是否显示无权限的模块/功能
    Private Function CheckShowAll() As String
        Dim returnValue As String
        If CheckAdmin() Then        ' 如果超级用户不受功能权限约束，显示所有系统
            returnValue = "true"
        Else
            If Application("ISSHOWFUNC_NOSEC") Is Nothing Then
                Application("ISSHOWFUNC_NOSEC") = ConfigurationSettings.AppSettings("ISSHOWFUNC_NOSEC").ToLower
            End If
            returnValue = Application("ISSHOWFUNC_NOSEC")       ' 是否显示没有权限的系统
        End If
        Return returnValue
    End Function
    '检测是否超级管理员
    Private Function CheckAdmin() As Boolean
        Return Mysoft.Map.Application.Security.User.IsAdmin(Session("UserGUID"))
    End Function
    '获得模块检索SQL
    Private Function GetApplicationSql() As String
        Dim returnValue As String

        If CheckShowAll() = "true" Then
            returnValue &= " SELECT ApplicationName,Application ,ApplicationIcon,IsSelectCompany,HierarchyCode,ParentHierarchyCode,'application' AS type,'FuncNav.aspx' as url,SelectBULevel" & _
                     " FROM myApplication" & _
                     " WHERE isDisabled='0'" & _
                     " AND Application IN ('" & VerifyApplication.GetLicenseApplicationString.Replace(",", "','") & "')" & _
                     " ORDER BY HierarchyCode"
        Else
            returnValue &= " SELECT DISTINCT a.ApplicationName,a.Application ,a.ApplicationIcon,a.IsSelectCompany,a.HierarchyCode,a.ParentHierarchyCode,'application' AS type,'FuncNav.aspx' as url,SelectBULevel" & _
                     " FROM myApplication a" & _
                     " INNER JOIN (select a.ParentHierarchyCode AS ParentHierarchyCode,a.Application" & _
                     "             from myStationUser su " & _
                     "             inner join myStationRights sr on sr.StationGUID=su.StationGUID " & _
                     "             inner join myFunction f on f.FunctionCode=sr.ObjectType " & _
                     "             inner join myApplication a on a.Application=f.Application  " & _
                     "             where su.userguid='" & Session("UserGUID") & "' " & _
                     "             group by a.ParentHierarchyCode,a.Application) AS Temp " & _
                     " ON (Temp.ParentHierarchyCode=a.HierarchyCode OR Temp.Application=a.Application) " & _
                     " WHERE a.isDisabled='0'" & _
                     " AND a.Application IN ('" & VerifyApplication.GetLicenseApplicationString.Replace(",", "','") & "')" & _
                     " ORDER BY a.HierarchyCode"
        End If

        Return returnValue
    End Function
    '获得功能检索SQL
    '参数 strAppCode 模块编码
    Private Function GetFunctionSql(ByVal strAppCode As String) As String
        Dim returnValue As String

        If CheckShowAll() = "true" Then
            returnValue = " SELECT FunctionName,FunctionCode,(select a.HierarchyCode from myApplication a where a.Application = myFunction.Application) + '.' +Hierarchycode as Hierarchycode,ParentHierarchycode,CASE WHEN IsNull(FunctionUrl,'')='' THEN '/funclist.aspx' ELSE FunctionUrl END AS FunctionUrl,FunctionIcon,ISNULL(UseScope,0) AS UseScope" & _
                     " FROM myfunction" & _
                     " WHERE IsDisabled = '0'" & _
                     " AND Application = '" & strAppCode.Replace("'", "''") & "'" & _
                     " AND [Level] <= 1 AND (1=1)" & _
                     " AND FunctionCode IN ('" & VerifyApplication.GetLicenseFunctionString(strAppCode, 0).Replace(",", "','") & _
                     "','" & VerifyApplication.GetLicenseFunctionString(strAppCode, 1).Replace(",", "','") & "')" & _
                     " AND IsDummy = 0" & _
                     " AND FunctionCode NOT IN ('" & VerifyApplication.GetNotInFilterString(1, strAppCode).Replace(",", "','") & "')" & _
                     " ORDER BY Hierarchycode"
        Else
            returnValue = " SELECT FunctionName,FunctionCode,(select a.HierarchyCode from myApplication a where a.Application = myFunction.Application) + '.' +Hierarchycode as Hierarchycode,ParentHierarchycode,CASE WHEN IsNull(FunctionUrl,'')='' THEN '/funclist.aspx' ELSE FunctionUrl END AS FunctionUrl,FunctionIcon,ISNULL(UseScope,0) AS UseScope" & _
                     " FROM myFunction" & _
                     " WHERE (1=1) AND FunctionGUID IN (select ParentFunctionGUID from myFunction " & _
                     " where IsDisabled = '0' and Application='" & strAppCode.Replace("'", "''") & "' AND level=1" & _
                     " and functioncode in (select srs.objectType from myStationRights srs inner join myStationUser su ON su.StationGUID=srs.StationGUID" & _
                     " where su.UserGUID='" & Session("UserGUID") & "'))" & _
                     " OR (1=1 and IsDisabled = '0' and Application='" & strAppCode & "' AND level=1 " & _
                     " AND FunctionCode in (select srs.objectType from myStationRights srs inner join myStationUser su ON su.StationGUID=srs.StationGUID" & _
                     " where su.UserGUID='" & Session("UserGUID") & "'))" & _
                     " AND FunctionCode IN ('" & VerifyApplication.GetLicenseFunctionString(strAppCode, 0).Replace(",", "','") & _
                     "','" & VerifyApplication.GetLicenseFunctionString(strAppCode, 1).Replace(",", "','") & "')" & _
                     " AND IsDummy = 0" & _
                     " AND FunctionCode NOT IN ('" & Mysoft.Map.Application.Security.VerifyApplication.GetNotInFilterString(1, strAppCode).Replace(",", "','") & "')" & _
                     " ORDER BY Hierarchycode"
        End If

        Return returnValue
    End Function
    '新增数据行
    Private Sub AddFuncNewRow(ByRef dtFunc As DataTable, ByVal name As String, ByVal url As String, ByVal code As String)
        Dim dr As DataRow

        dr = dtFunc.NewRow()
        dr("Name") = name
        dr("Url") = url
        dr("Code") = code

        dtFunc.Rows.Add(dr)
    End Sub
    Private Function GetFunctionTableCache() As DataTable
        Dim returnValue As DataTable
        If MyCache.Get("FunctionTableCache") IsNot Nothing Then
            returnValue = MyCache.Get("FunctionTableCache")
        End If
        Return returnValue
    End Function
    '获得功能数据集
    Private Function GetFunctionDataTable() As DataTable
        Dim returnValue As DataTable        '模块/功能数据集
        Dim dtApp As DataTable              '模块数据集
        Dim dtFunc As DataTable             '功能数据集
        Dim stParentAppCode As String       '父模块编码
        Dim strAppCode As String            '模块编码
        Dim url As String                   '链接
        Dim name As String                  '名称
        Dim code As String                  '层级编码

        If MyCache.Get("MyDesktop_FunctionTable") Is Nothing Then
            returnValue = GetFuncDataTableConfig()
            dtApp = GetApplicationDataTable()
            If Not dtApp Is Nothing AndAlso dtApp.Rows.Count > 0 Then
                For Each appDr As DataRow In GetApplicationDataTable().Rows
                    stParentAppCode = Convert.ToString(appDr("ParentHierarchyCode"))
                    code = Convert.ToString(appDr("HierarchyCode"))
                    name = Convert.ToString(appDr("ApplicationName"))
                    url = Convert.ToString(appDr("url"))
                    AddFuncNewRow(returnValue, name, url, code)
                    If stParentAppCode = "" Or stParentAppCode = String.Empty Then
                        Continue For
                    Else
                        strAppCode = Convert.ToString(appDr("Application"))
                        dtFunc = MyDB.GetDataTable(GetFunctionSql(strAppCode))
                        If Not dtFunc Is Nothing AndAlso dtFunc.Rows.Count > 0 Then
                            For Each funcDr As DataRow In dtFunc.Rows
                                code = Convert.ToString(funcDr("HierarchyCode"))
                                name = Convert.ToString(funcDr("FunctionName"))
                                url = Convert.ToString(funcDr("FunctionUrl"))
                                AddFuncNewRow(returnValue, name, url, code)
                            Next funcDr
                        End If
                    End If
                Next appDr
                MyCache.Insert("MyDesktop_FunctionTable", returnValue, MyCache.DayFactor)
            End If
        Else
            returnValue = MyCache.Get("MyDesktop_FunctionTable")
        End If

        Return returnValue
    End Function
    '获得模块数据集
    Private Function GetApplicationDataTable() As DataTable
        Return MyDB.GetDataTable(GetApplicationSql)
    End Function
    '获得功能数据集结构
    Private Function GetFuncDataTableConfig()
        Dim returnValue As DataTable
        returnValue = New DataTable("FunctionList")

        Dim dc As DataColumn

        dc = New DataColumn
        dc.DataType = System.Type.GetType("System.String")
        dc.ColumnName = "Name"
        returnValue.Columns.Add(dc)

        dc = New DataColumn
        dc.DataType = System.Type.GetType("System.String")
        dc.ColumnName = "Url"
        returnValue.Columns.Add(dc)

        dc = New DataColumn
        dc.DataType = System.Type.GetType("System.String")
        dc.ColumnName = "Code"
        returnValue.Columns.Add(dc)

        Return returnValue
    End Function
End Class